package com.VolunteerPlatform.Controller.team;

import com.VolunteerPlatform.Entity.BankInfo;
import com.VolunteerPlatform.Entity.TeamInfo;
import com.VolunteerPlatform.Service.TeamService.TeamInfoService;
import com.VolunteerPlatform.Service.VolunteerService.BankInfoService;
import com.VolunteerPlatform.Service.VolunteerService.VolunteerInfoService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by DoveXL on 2018/12/9.
 */
@Controller
@RequestMapping(value = "/TeamInfo/**")
public class TeamInfoController {


    @Autowired
    private TeamInfoService teamInfoService;
    @Autowired
    private VolunteerInfoService volunteerInfoService;
    @Autowired
    private BankInfoService bankInfoService;


    @RequestMapping(value = "CertainTeamInfo", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getBankInfo(@RequestBody String jsondata, HttpSession httpSession) {

        int rowCount=0;
        SecurityContextImpl securityContextImpl = (SecurityContextImpl) httpSession.getAttribute("SPRING_SECURITY_CONTEXT");
        String User = securityContextImpl.getAuthentication().getName();
        JSONObject jsonObject = JSON.parseObject(jsondata);
        int page = jsonObject.getInteger("page");
        int pagesize = jsonObject.getIntValue("pagesize");
        String TeamName = jsonObject.getString("TeamName");
        JSONArray UserInfoArray = new JSONArray();
        List<TeamInfo> teamInfoList = teamInfoService.getAllTeamInfoService(TeamName,-1,-1);
        try {
            rowCount = teamInfoList.size();
            if(rowCount%pagesize!=0)
            {
                rowCount = rowCount / pagesize + 1;
            }
            else
            {
                rowCount = rowCount / pagesize;
            }
            List<TeamInfo> TeamInfoList = teamInfoService.getAllTeamInfoService(TeamName,(page-1)*pagesize,pagesize);

            for (int i=0;i<TeamInfoList.size();i++) {
                JSONObject UserInfo = new JSONObject();
                UserInfo.put("user_teamname", TeamInfoList.get(i).getTeamName());
                UserInfo.put("user_teamnumber", TeamInfoList.get(i).getTeamNumber());
                UserInfo.put("user_teamleading", TeamInfoList.get(i).getTeamLeading());
                UserInfo.put("user_leadingphonenum", TeamInfoList.get(i).getLeadingPhonenum());
                UserInfoArray.add(UserInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("pagecount",rowCount);
        jsonObject1.put("currentpage",page);
        jsonObject1.put("pagelist",UserInfoArray);
        return JSON.toJSONString(jsonObject1);
    }



    @RequestMapping(value = "CheckTeamName", method = RequestMethod.POST)
    @ResponseBody
    public String CheckTeamName(@RequestBody String jsondata) {
        JSONObject jsonObject = JSON.parseObject(jsondata);
        String TeamName = jsonObject.getString("TeamName");
        List<TeamInfo> teamInfoList = teamInfoService.getAllTeamInfoService(TeamName,-1,-1);
        if (teamInfoList.size()>=1)
        {
            return "false";
        }else if (teamInfoList.size()==0)
        {
            return "true";
        }else
        {
            return "False";
        }
    }


    @RequestMapping(value = "AddTeamInfo", method = RequestMethod.POST)
    @ResponseBody
    public String AddTeamInfo(@RequestBody String jsondata) {
        JSONObject jsonObject = JSON.parseObject(jsondata);
        String TeamName = jsonObject.getString("TeamName");
        String TeamLeading = jsonObject.getString("TeamLeading");
        String TeamLeadingPhonenum = jsonObject.getString("TeamLeadingPhonenum");
        int flag = teamInfoService.insertTeamInfoService(TeamName,0,TeamLeading,TeamLeadingPhonenum);
        if (flag==1)
        {
            return "true";
        }else
        {
            return "false";
        }
    }


    @RequestMapping(value = "ChangeTeamInfo", method = RequestMethod.POST)
    @ResponseBody
    public String ChangeTeamInfo(@RequestBody String jsondata) {
        JSONObject jsonObject = JSON.parseObject(jsondata);
        String OriginTeamName = jsonObject.getString("OriginTeamName");
        String NewTeamName = jsonObject.getString("NewTeamName");
        String TeamLeading = jsonObject.getString("TeamLeading");
        String TeamLeadingPhonenum = jsonObject.getString("TeamLeadingPhonenum");
        System.out.println(OriginTeamName);
        int flag = teamInfoService.updateTeamInfoService(OriginTeamName,NewTeamName,-1,TeamLeading,TeamLeadingPhonenum);
        if (flag==1)
        {
            return "true1";
        }else
        {
            return "false1";
        }
    }


    @RequestMapping(value = "DeleteTeamInfo", method = RequestMethod.POST)
    @ResponseBody
    public String DeleteTeamInfo(@RequestBody String jsondata) {
        JSONObject jsonObject = JSON.parseObject(jsondata);
        String TeamName = jsonObject.getString("TeamName");
        int flag = teamInfoService.deleteTeamInfoService(TeamName);
        if (flag==1)
        {
            return "true2";
        }else
        {
            return "false2";
        }
    }



    @RequestMapping(value = "ImportTeamInfo", method = RequestMethod.POST)
    @ResponseBody
    public String ImportTeamInfo(@RequestBody String jsondata,HttpSession httpSession) throws IOException{

        JSONObject jsonObject = JSON.parseObject(jsondata);
        String FileName = jsonObject.getString("FileName");
        httpSession.setAttribute("ErrorName","错误"+FileName.substring(0,FileName.lastIndexOf("."))+".xls");
        int number =teamInfoService.ImportBatchTeamInfoService(FileName);
        return String.valueOf(number);
    }



    @RequestMapping(value = "ExportTeamInfo", method = RequestMethod.POST)
    @ResponseBody
    public String ExportTeamInfo(@RequestBody String jsondata,HttpSession httpSession) throws IOException{

        JSONObject jsonObject = JSON.parseObject(jsondata);
        String checkFieldName = jsonObject.getString("checkFieldName");
        List<String> checkFieldNameList = JSON.parseArray(checkFieldName,String.class);
        List<String> checkFieldChineseNameList = new ArrayList<>();
        Map<String,String> TeamInfoChineseMap = new HashMap<>();
        TeamInfoChineseMap.put("Number","序号");
        TeamInfoChineseMap.put("TeamName","单位名称");
        TeamInfoChineseMap.put("TeamNumber","单位人数");
        TeamInfoChineseMap.put("TeamLeading","单位负责人");
        TeamInfoChineseMap.put("LeadingPhonenum","联系方式");
        String title = "单位名称一览表";
        String[] rowsName = new String[checkFieldNameList.size()];
        for(int i =0;i<checkFieldNameList.size();i++)
        {
            checkFieldChineseNameList.add(TeamInfoChineseMap.get(checkFieldNameList.get(i)));
        }
        checkFieldChineseNameList.toArray(rowsName);
        int flag =0;
        String filePath = "D:\\code\\IntelIJ IDEA\\GeoLife\\LongHuaVolunteer\\target\\com.ecust\\files\\download\\";
        String fileName = "teaminfo"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()).toString() +".xls";
        flag =teamInfoService.ExportTeamInfoService(title,rowsName,checkFieldNameList,filePath+fileName);
        httpSession.setAttribute("ExportTeamInfoName",fileName);
        return String.valueOf(flag);
    }

}
